#include"imecc_colorconversions.h"
#include"..\..\include\imp_typedefine.h"
#include"..\..\include\imp_io_wrapper.h"
void impcodec_RGB24toYuv420(Int8 *rgbBuf, Int32 width, Int32 height,
							Int8 *yBuf, Int8 *uBuf, Int8 *vBuf, Int32
							srcStride,Int32 dstStride)
{
	ime_CC_RGB32toYV12_C((UInt8*)rgbBuf, width, height, (UInt8*)yBuf, (UInt8*)uBuf, (UInt8*)vBuf, srcStride, dstStride); 
}

/*RGB565RGB888(Int8 *p565, Int8 *p888, Int32 m_nWidth, Int32 m_nHeight) {
	Int32 i, j;
	unsigned short rgb565;
	Int8 r5, g6, b5;

	for(i=0; i<m_nHeight; i++)
		for(j=0; j<m_nWidth; j++)
		{
			rgb565 = *(unsigned short *)p565;
			r5 = (rgb565 >> 8) & 0xf8;
			g6 = (rgb565 >> 3) & 0xf8;
			b5 = (rgb565 << 3) & 0xf8;

			*p888++ = b5;
			*p888++ = g6;
			*p888++ = r5;

			p565 += 2;
		}

		return IMP_False;
}*/


ImpBool  RGB565RGB888(Int8 *p565, Int8 *p888, Int32 m_nWidth, Int32 m_nHeight) {
	Int32 i, j;
	unsigned short rgb565;
	Int8 r5, g6, b5;
    Int32 x,y;
	Int8 *pPixel = NULL;
	for(i=0; i<m_nHeight; i++)
		for(j=0; j<m_nWidth; j++)
		{
			rgb565 = *(unsigned short *)p565;
			r5 = (rgb565 >> 8) & 0xf8;
			g6 = (rgb565 >> 3) & 0xf8;
			b5 = (rgb565 << 3) & 0xf8;
            x = m_nHeight - 1 - i;
			y = j;
            pPixel = p888 + (x*m_nWidth + y)*3; 
			*pPixel++ = b5;
			*pPixel++ = g6;
			*pPixel++ = r5;

			p565 += 2;
		}

		return IMP_False;

}
/*RGB565RGB888(Int8 *p565, Int8 *p888, Int32 m_nWidth, Int32 m_nHeight) {
	Int32 i, j;
	unsigned short rgb565;
	Int8 r5, g6, b5;
	Int8 *pEnd888 = p888 + m_nWidth*m_nHeight*3-1;
	for(i=0; i<m_nHeight; i++)
		for(j=0; j<m_nWidth; j++)
		{
			rgb565 = *(unsigned short *)p565;
			r5 = (rgb565 >> 8) & 0xf8;
			g6 = (rgb565 >> 3) & 0xf8;
			b5 = (rgb565 << 3) & 0xf8;

			*pEnd888-- = b5;
			*pEnd888-- = g6;
			*pEnd888-- = r5;

			p565 += 2;
		}

		return IMP_False;
}*/

void ime_CC_RGB565toYV12_C( unsigned char *RGB565Buffer, int ImageWidth, int ImageHeight,
						   unsigned char *YBuffer, unsigned char *UBuffer, unsigned char *VBuffer, int SrcPitch,int DstPitch )
{
	Int8 *pBufferRGB888 = (Int8 *)IMP_Malloc(ImageWidth*ImageHeight*3);
    RGB565RGB888(RGB565Buffer, pBufferRGB888, ImageWidth, ImageHeight);
	impcodec_RGB24toYuv420(pBufferRGB888, ImageWidth, ImageHeight, (UInt8*)YBuffer, 
		(UInt8*)UBuffer, (UInt8*)VBuffer, SrcPitch, DstPitch );
	IMP_Free(pBufferRGB888);
}

//void ime_CC_RGB565toYV12_C(Int8 *pic_rgb565, Int32 win, Int32 hin, Int8 *pic_yuv)
//{
//	if(win==wout && hin==hout)
//	{
//        //Int8 *pBufferRGB888 = (Int8 *)IMP_Malloc(win*hin*4);
//		RGB565RGB888(pic_rgb565, pBufferRGB888, win, hin);
//		impcodec_RGB24toYuv420(pBufferRGB888, win, hin, pic_yuv, 
//			pic_yuv+win*hin, pic_yuv+win*hin+win*hin/4, win*3, win );
//		//IMP_Free(pBufferRGB888);
//	}
//	//else
//	//{
//	//	//Int8 *pBufferRGB888 = (Int8 *)IMP_Malloc(win*hin*4);
//	//	//Int8 *pBufferTemp = (Int8 *)IMP_Malloc(win*hin*4);
//	//	RGB565RGB888(pic_rgb565, pBufferRGB888, win, hin);
//	//	impcodec_RGB24toYuv420(pBufferRGB888, win, hin, pBufferTemp, 
//	//		pBufferTemp+win*hin,
//	//		BufferTemp+win*hin+win*hin/4, win*3, win );
//	//	//IMP_Free(pBufferRGB888);
//	//	//IMP_Free(pBufferTemp);
//	//	//impcodec_shrink_yuv420(m_pBufferTemp, win, hin, pic_yuv, wout, hout);
//	//}
//	return IMP_False;
//}